package conchmusic.conchmusicclient.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import conchmusic.conchmusicclient.R;
import conchmusic.conchmusicclient.activity.PlayActivity;


/**
 * Created by sbml1 on 2017/4/23.
 */

public class PlayBar extends RelativeLayout{
    private MsgReceiver msgReceiver;
    private ImageView imageView;
    private TextView textView;
    private Button btn_next;
    private Button btn_play;
    private Button btn_previous;
    private String[] list;
    private int localSongId;
    private int posit;
    private boolean playing=false;
    private String localPictureUrlTemp="blank";

    public PlayBar(Context context) {
        super(context);
    }

    public PlayBar(Context context, AttributeSet attributeSet){
        super(context,attributeSet);
        LayoutInflater.from(context).inflate(R.layout.public_playerbar,this);
        imageView=(ImageView)findViewById(R.id.playbar_image);
        textView=(TextView)findViewById(R.id.playbar_name);
        btn_next=(Button)findViewById(R.id.playbar_next);
        btn_play=(Button)findViewById(R.id.playbar_play);
        btn_previous=(Button)findViewById(R.id.playbar_previous);

        imageView.setOnClickListener(PlayBarListener);
        btn_next.setOnClickListener(PlayBarListener);
        btn_play.setOnClickListener(PlayBarListener);
        btn_previous.setOnClickListener(PlayBarListener);

        setImageView("");
        setTextView("info");
    }
    //启动MsgReceiver接受广播
    public void startMsgReceiver(){
        msgReceiver=new MsgReceiver();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("conchumusic.conchumusicclient.RECEIVER");
        this.getContext().registerReceiver(msgReceiver,intentFilter);
    }

    public void closeMsgReceiver(){
        this.getContext().unregisterReceiver(msgReceiver);
    }


    private OnClickListener PlayBarListener=new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.playbar_image:
                    toPlayActivity(v);
                    break;
                case R.id.playbar_previous:
                    previous(v);
                    break;
                case R.id.playbar_play:
                    play(v);
                    break;
                case R.id.playbar_next:
                    next(v);
                    break;
            }
        }
    };


    //设置专辑封面
    public void setImageView(String fileName){
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeFile(fileName);
            imageView.setImageBitmap(bitmap);
        } catch (Exception e) {
            imageView.setImageResource(R.mipmap.ic_disc);
        }
        if(bitmap==null){
            imageView.setImageResource(R.mipmap.ic_disc);
        }
    }

    //设置歌名
    public void setTextView(String text){
        textView=(TextView)findViewById(R.id.playbar_name);
        textView.setText(text);
    }

    //播放控制
    private void musicControl(String action,int posit,String[] list){
        Intent intent=new Intent();
        intent.putExtra("action",action);
        intent.putExtra("posit",posit);
        intent.putExtra("songList",list);
        intent.setAction("music2");
        intent.setPackage(this.getContext().getPackageName());
        this.getContext().startService(intent);

    }

    //播放按钮
    public void play(View view){
        if(playing){
            musicControl("pause",-1,list);
//            playing=false;
//            btn_play.setBackgroundResource(R.mipmap.ic_play);
        }else{
            musicControl("play",-1,list);
//            playing=true;
//            btn_play.setBackgroundResource(R.mipmap.ic_pause);
        }
    }

    public void next(View view){
        musicControl("next",-1,list);
//        playing=true;
//        btn_play.setBackgroundResource(R.mipmap.ic_pause);
    }

    public void previous(View view){
        musicControl("previous",-1,list);
//        playing=true;
//        btn_play.setBackgroundResource(R.mipmap.ic_pause);
    }

    //跳转到playactivity
    public void toPlayActivity(View view) {
        Intent intent=new Intent();
        intent.setClass(this.getContext(),PlayActivity.class);
        intent.putExtra("songList",list);
        intent.putExtra("posit",posit);
        this.getContext().startActivity(intent);
    }


    public int getLocalSongId(){
        //Log.i("playbar_localSongId",localSongId+".......");
        return localSongId;
    }

    //接收广播
    public class MsgReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            //设置歌曲信息
            localSongId=intent.getIntExtra("localSongId",0);
            String title=intent.getStringExtra("title");
            textView.setText(title);

            //设置图片
            String localPictureUrl=intent.getStringExtra("localPictureUrl");
            if(localPictureUrl!=null && !localPictureUrlTemp.equals(localPictureUrl)){
                setImageView(localPictureUrl);
                localPictureUrlTemp=localPictureUrl;
            }

            //追踪songList和posit
            list=intent.getStringArrayExtra("songList");
            //Log.i("playbar list.size",list.length+"...");
            posit=intent.getIntExtra("posit",0);

            //追踪播放状态
            playing=intent.getBooleanExtra("playing",false);
            if(playing){
                btn_play.setBackgroundResource(R.mipmap.ic_pause);
            }else{
                btn_play.setBackgroundResource(R.mipmap.ic_play);
            }
        }
    }
}
